const Koa = require('koa');
const cityRouter = require('./router/cityRouter');
const cinemaRouter = require('./router/cinemaRouter');
const filmRouter = require('./router/filmRouter');
const videoRouter = require('./router/videoRouter');
const consultRouter = require('./router/consultRouter');
const koaStatic = require('koa-static');
const koaBody = require('koa-body');

const app = new Koa();
// 设置静态目录
app.use(koaStatic(__dirname + '/public'));
app.use(koaBody());


// 中间件,ctx.params接收参数, 不管是get请求还是post请求
app.use(async (ctx, next) => {
    ctx.params = {
        ...ctx.query,
        ...ctx.request.body
    }
    await next();
})


// 跨域设置
app.use(async (ctx, next) => {
    ctx.set("Access-Control-Allow-Origin", "*");
    ctx.set("Access-Control-Allow-Methods", "PUT, POST, GET, DELETE, OPTIONS");
    // 请求头设置
    ctx.set(
        "Access-Control-Allow-Headers",
        `Content-Type, Content-Length, Authorization, Accept, X-Requested-With , yourHeaderFeild,x-token,sessionToken,token`
    );
    if (ctx.method == "OPTIONS") {
        ctx.body = 200;
    } else {
        await next();
    }
})

// 使用路由
app.use(cityRouter.routes());
app.use(cinemaRouter.routes());
app.use(filmRouter.routes());
app.use(videoRouter.routes());
app.use(consultRouter.routes());





app.listen(3009, () => {
    console.log('服务已启动, 在http://localhost:3009')
});